package com.sanrenzu.everybodytv.db;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.TableUtils;

import java.sql.SQLException;
import java.util.Collection;

/**
 * Created by zhangke on 16/7/16.
 * 创建帮助工具类
 * 创建数据库
 * 创建表格
 * 版本更新
 * 步骤
 * 1继承
 *  OrmLiteSqliteOpenHelper
 *  2 重写方法
 *  3添加构造
 *  4 创建表格
 *
 *
 *
 */
public class ORMliteHelper extends OrmLiteSqliteOpenHelper {
    private static final  String DATA_BASE_NAME="shopping.db";
    private static final  String TAG="TAG";


    /**
     * @param context'
     *
     *            创建数据库
     */
    public ORMliteHelper(Context context) {
        super(context, DATA_BASE_NAME, null, 1);
    }

    @Override  //创建表格
    public void onCreate(SQLiteDatabase database, ConnectionSource connectionSource) {
        try {
            int state= TableUtils.createTable(connectionSource, Collection.class);
            Log.i(TAG, "onCreate: "+(state!=-1?"创建成功":"创建失败"));
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }

    @Override
    public void onUpgrade(SQLiteDatabase database, ConnectionSource connectionSource, int oldVersion, int newVersion) {
        try {
            TableUtils.dropTable(connectionSource,Collection.class,true);
            onCreate(database,connectionSource);
        } catch (SQLException e) {
            e.printStackTrace();
        }


    }
}
